import { http } from '@/utils'
import { useGlobalSetting } from '@/settings/system'
import { TenantTreeNode } from './TreeTyping'

const { appCode: baseAppCode } = useGlobalSetting()

export function getTenantListApi(tenantName = '', appCode = baseAppCode) {
  return http.get<TenantTreeNode>(
    '/paas/appClient/getTenantApps',
    { appCode, tenantName, version: '' },
    { ignoreCheckToken: true, ignoreHandleError: true }
  )
}

export function treeFilter(data: TenantTreeNode): TenantTreeNode[] {
  if (data.tenantApps?.length) {
    return [data]
  } else if (data.children) {
    return data.children.map((node) => treeFilter(node)).flat(4)
  } else {
    return []
  }
}
